import { ApiProperty } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
import { IsBoolean, IsEnum, IsOptional, IsString, IsUUID, Min, IsArray } from 'class-validator';
import { ContentType } from '../../common/enums';

export class QueryContentDto {
  @ApiProperty({
    description: '根据内容标题进行模糊查询',
    required: false,
  })
  @IsString()
  @IsOptional()
  title?: string;

  @ApiProperty({
    description: '根据内容类型过滤',
    enum: ContentType,
    required: false,
  })
  @IsEnum(ContentType)
  @IsOptional()
  contentType?: ContentType;

  @ApiProperty({
    description: '根据创建者过滤内容',
    format: 'uuid',
    required: false,
  })
  @IsUUID()
  @IsOptional()
  createdBy?: string;

  @ApiProperty({
    description: '根据模板 ID 查询内容',
    format: 'uuid',
    required: false,
  })
  @IsUUID()
  @IsOptional()
  templateId?: string;

  @ApiProperty({
    description: '根据审核状态过滤内容',
    required: false,
  })
  @IsBoolean()
  @IsOptional()
  @Transform(({ value }) => {
    if (value === 'true') return true;
    if (value === 'false') return false;
    return value;
  })
  isApproved?: boolean;

  @ApiProperty({
    description: '是否包含已删除的内容',
    default: false,
    required: false,
  })
  @IsBoolean()
  @IsOptional()
  @Transform(({ value }) => {
    if (value === 'true') return true;
    if (value === 'false') return false;
    return value;
  })
  includeDeleted?: boolean = false;

  @ApiProperty({
    description: '页码，从1开始',
    minimum: 1,
    default: 1,
    required: false,
  })
  @Type(() => Number)
  @Min(1)
  @IsOptional()
  page?: number = 1;

  @ApiProperty({
    description: '每页数量',
    minimum: 1,
    maximum: 100,
    default: 20,
    required: false,
  })
  @Type(() => Number)
  @Min(1)
  @IsOptional()
  pageSize?: number = 20;

  @ApiProperty({
    description: '根据标签ID列表过滤内容（AND条件，多个ID用逗号分隔）',
    required: false,
  })
  @IsOptional()
  @Transform(({ value }) => {
    if (!value) return undefined;
    return value.split(',').map((id) => id.trim());
  })
  @IsArray()
  @IsUUID('4', { each: true })
  tagIds?: string[];
}
